<?php
if (!defined('IN_PHPBB'))
{
	exit;
}

// Initiate.
molten_chat_phpbb_hook::hook();

class molten_chat_phpbb_hook
{
	public static function hook()
	{
		global $phpbb_hook;

		// Register self.
		$phpbb_hook->register('phpbb_user_session_handler', array(__CLASS__, 'initiate'));
	}

	public static function initiate()
	{
		global $config, $phpEx, $phpbb_hook, $phpbb_root_path, $template, $user;

		$methods = array('mcp', 'posting');
		$working = false;

		// This data will be configurable inside of the administrator control panel in the future.
		$parameters = array(
			'room' => 'system_index', // The name of the room.
			'levels' => 5, // The maximum levels stored on disk per room.
			'messages' => 50, // The maximum messages stored per level.
			'retrieve' => 100, // The maximum messages to retrieve for fresh sessions (this should not be more than [(levels - 1) * messages]).
			'age' => 300, // The maximum age (in seconds) of a level before it is deleted.
			'update' => 30 // The amount of time that must pass before an updated user list is sent if no more users join or leave the room.
		);

		// Add the language variables for the chat program to our environment.
		$user->add_lang('mods/molten/chat/display');

		if (isset($config['molten_chat_installed']) and !empty($config['molten_chat_enable']))
		{
			// Set includes directory to the proper location.
			define('molten_chat_include', $phpbb_root_path . 'includes/molten_chat/', true);

			// Include chat program.
			require_once(molten_chat_include . 'molten_chat_server.' . $phpEx);

			if (extension_loaded(molten_chat_server::$configuration['cache']))
			{
				$working = true;

				// Create the room (if it doesn't exist).
				molten_chat_server::create($parameters['room']);

				// Connect to the room.
				molten_chat_server::connect($parameters['room'], false, false, $parameters['levels'], $parameters['messages'], $parameters['retrieve'], $parameters['age'], $parameter['update']);
			}
		}

		// Add template variables.
		$template->assign_vars(array(
			'MOLTEN_CHAT_POPOUT' => append_sid($phpbb_root_path . 'molten_chat_room.' . $phpEx, 'build', false),
			'MOLTEN_CHAT_RETRIEVE' => append_sid($phpbb_root_path . 'molten_chat_retrieve.' . $phpEx, 'build', false),
			'MOLTEN_CHAT_SEND' => append_sid($phpbb_root_path . 'molten_chat_send.' . $phpEx, 'build', false),
			'MOLTEN_CHAT_USERNAME' => $user->data['username'],
			'S_MOLTEN_CHAT_ENABLED' => $working
		));

		// Run post alert system.
		$on_page = array();
		preg_match('#^([a-z/_]+)#i', $user->page['page_name'], $on_page);
		if (sizeof($on_page))
		{
			if (in_array($on_page[1], $methods))
			{
				// Register aditional hooks.
				$phpbb_hook->register('exit_handler', array(__CLASS__, 'hook_' . $on_page[1]));
			}
		}
	}

	public static function hook_posting()
	{
		global $auth, $config, $data, $error, $redirect_url, $mode, $post_data, $submit, $user;

		$message = false;
		$regex = '/(\\.|!|\\?)$/';

		if ($submit and !sizeof($error) and (((!$config['enable_queue_trigger'] or $user->data['user_posts'] > $config['queue_trigger_posts']) and $auth->acl_get('f_noapprove', $data['forum_id'])) or $auth->acl_get('m_approve', $data['forum_id'])))
		{
			$uri = str_replace('&amp;', '&', $redirect_url);
			$uri = preg_replace('#&sid\\=[0-9a-f]{32,32}#i', '', $uri);
			$uri = preg_replace('#\\?sid\\=[0-9a-f]{32,32}&#i', '?', $uri);

			switch ($mode)
			{
				// New reply.
				case 'reply':
				case 'quote':
					if ($config['molten_chat_messages_reply'])
					{
						$title = preg_match ($regex, $post_data['post_subject']) ?
							$post_data['post_subject'] :
							$post_data['post_subject'] . '.';
						$input = array(
							$user->data['username'],
							$title
						);

						$message = '/link local=true message="' . vsprintf($user->lang['MOLTEN_CHAT_NEW_REPLY'], $input) . '" system=true uri="' . $uri . '"';
					}
				break;

				// New topic.
				case 'post':
					if ($config['molten_chat_messages_topic'])
					{
						$title = preg_match ($regex, $post_data['post_subject']) ?
							$post_data['post_subject'] :
							$post_data['post_subject'] . '.';
						$input = array(
							$user->data['username'],
							$title
						);

						$message = '/link local=true message="' . vsprintf($user->lang['MOLTEN_CHAT_NEW_TOPIC'], $input) . '" system=true uri="' . $uri . '"';
					}
				break;
			}

			if ($message)
			{
				// Send the message; the chat server will take care of the rest.
				molten_chat_server::send($message, null, false, true);
			}
		}

		return;
	}

	public static function hook_mcp()
	{
		return true;
	}
}
?>